package com.youth.hrsync.config.batch;

import com.youth.hrsync.bean.xian.XianUserinfo;
import com.kcis.xian.hrsync.utils.DateUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class HrSyncBatchConfig extends DefaultBatchConfig {

    private Logger logger = LoggerFactory.getLogger(HrSyncBatchConfig.class);

    @Autowired
    SqlSessionFactory xianSqlSessionFactory;

    @Autowired
    SqlSessionTemplate kunshanSqlSessionTemplate;

//    @Autowired
//    KunShanUserinfoMapper kunShanUserinfoMapper;
//    @Autowired
//    KunShanCheckinoutMapper kunShanCheckinoutMapper;
//
//    @Autowired
//    XianUserinfoMapper xianUserinfoMapper;
//
//    @Autowired
//    XianCheckinoutMapper xianCheckinoutMapper;


    @Bean
    public MyBatisCursorItemReader<XianUserinfo> hrUserItemMybatisReader() {
        MyBatisCursorItemReader<XianUserinfo> reader = new MyBatisCursorItemReader<XianUserinfo>();
        reader.setSqlSessionFactory(xianSqlSessionFactory);
        reader.setQueryId("com.kcis.xian.hrsync.mapper.xian.XianUserinfoMapper.selectUsers");
        return reader;
    }

    @Bean
    public MyBatisBatchItemWriter hrUserItemMybatisWriter() {
        MyBatisBatchItemWriter writer = new MyBatisBatchItemWriter();
        writer.setSqlSessionTemplate(kunshanSqlSessionTemplate);
        writer.setStatementId("com.kcis.xian.hrsync.mapper.kunshan.KunShanUserinfoMapper.insert");
        return writer;
    }

    @Bean
    public Step syncUserStep() {
        return stepBuilderFactory.get("syncUserStep")
                .chunk(100)
                .reader(hrUserItemMybatisReader())
                .writer(hrUserItemMybatisWriter())
                .build();
    }

    @Bean
    public MyBatisCursorItemReader<XianUserinfo> hrCheckinoutItemMybatisReader() {
        MyBatisCursorItemReader<XianUserinfo> reader = new MyBatisCursorItemReader<XianUserinfo>();
        reader.setSqlSessionFactory(xianSqlSessionFactory);
        reader.setQueryId("com.kcis.xian.hrsync.mapper.xian.XianCheckinoutMapper.selectCheckinouts");
        Map<String , Object> map = new HashMap<>();
        Date startTime = DateUtils.getBackwardDateByNum(new Date(), 13);
        map.put("time" , startTime);
        reader.setParameterValues(map);
        return reader;
    }

//    public static void main(String[] args) {
//        Date startTime = DateUtils.getBackwardDateByNum(new Date(), 13);
//        System.out.println(startTime);
//    }

    @Bean
    public MyBatisBatchItemWriter hrCheckinoutItemMybatisWriter() {
        MyBatisBatchItemWriter writer = new MyBatisBatchItemWriter();
        writer.setSqlSessionTemplate(kunshanSqlSessionTemplate);
        writer.setStatementId("com.kcis.xian.hrsync.mapper.kunshan.KunShanCheckinoutMapper.insert");
        return writer;
    }

    @Bean
    public Step syncCheckinoutStep() {
        return stepBuilderFactory.get("syncCheckinoutStep")
                .chunk(100)
                .reader(hrCheckinoutItemMybatisReader())
                .writer(hrCheckinoutItemMybatisWriter())
                .build();
    }

    @Bean
    public Flow syncFlow() {
        return new FlowBuilder<Flow>("syncFlow")
                .start(syncUserStep())
                .next(syncCheckinoutStep())
                .build();
    }

    @Bean
    public Step syncStep() {
        return stepBuilderFactory.get("syncStep")
                .flow(syncFlow())
                .build();
    }

}
